package pers.li.dao;

import pers.li.model.Goods;
import pers.li.model.Pic;
import pers.li.utils.DataProcess;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class GoodsDao {
    private final DataProcess dp = new DataProcess(true);

    public List<Goods> getToday() {
        String sql = "select *from t_goods where istoday = 1";
        return dp.getObjectList(sql, Goods.class);
    }

    public List<Goods> getGoodsByCateId(Integer cateId) {
        String sql = "select *from t_goods where cate_id = ?";
        return dp.getObjectList(sql, Goods.class, cateId);
    }

    public List<Goods> getGoodsByCateIdSort(Integer cateId, Comparator<? super Goods> comparator) {
        String sql = "select *from t_goods where cate_id = ?";
        List<Goods> goodsList = dp.getObjectList(sql, Goods.class, cateId);
        if (goodsList == null)
            return null;
        return goodsList.stream().sorted(comparator).collect(Collectors.toList());
    }

    public Goods getGoodsByGoodId(Integer goodId) {
        String sql = "select *from t_goods where goods_id = ?";
        return dp.getObject(sql, Goods.class, goodId);
    }

    public List<Goods> getHotGoods(int num) {
        String sql = "SELECT *FROM t_goods ORDER BY goods_sales DESC";
        return dp.getObjectList(sql, Goods.class).stream()
                .limit(num)
                .collect(Collectors.toList());
    }

    public List<Goods> getNewGoods(int num) {
        String sql = "SELECT *FROM t_goods ORDER BY goods_date DESC";
        return dp.getObjectList(sql, Goods.class).stream()
                .limit(num)
                .collect(Collectors.toList());
    }

    public List<Pic> getPicByGoodId(Integer goodId) {
        String sql = "SELECT pic_id,pic_url FROM t_pic WHERE goods_id = ?";
        return dp.getObjectList(sql, Pic.class, goodId);
    }

    public List<Goods> getGoodsByKeyword(String keyWord) {
        String sql = "select * from t_goods where goods_name like '%" + keyWord + "%'";
        return dp.getObjectList(sql, Goods.class);
    }


}
